package com.universest.swordholder.ui.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.universest.swordholder.SwordHolderApplication
import com.universest.swordholder.ui.item.ConfigItemWidget
import com.universest.swordholder.ui.theme.SwordHolderTheme
import com.universest.swordholder.viewmodel.ConfigScreenViewModel
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.universest.swordholder.R

@Composable
fun ConfigScreen(modifier: Modifier = Modifier, viewModel: ConfigScreenViewModel = viewModel()) {
    val context = LocalContext.current
    Column(modifier = modifier.background(MaterialTheme.colorScheme.background)) {
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp)
            .height(80.dp),
            verticalAlignment = Alignment.CenterVertically) {
            Icon(
                imageVector = Icons.Default.Settings,
                contentDescription = null,
                modifier = Modifier.size(30.dp),
                tint = MaterialTheme.colorScheme.onBackground
            )
            Text(text = stringResource(id = R.string.app_settings),
                fontWeight = FontWeight.Bold,
                fontSize = 25.sp,
                color = MaterialTheme.colorScheme.onBackground)
        }
        viewModel.configItems.forEachIndexed { index, item ->
            val value by viewModel.getUserConfig(item.key, item.defaultValue)
                .collectAsStateWithLifecycle(item.defaultValue)
            ConfigItemWidget(configItem = item, modifier = Modifier.padding(bottom = 10.dp), value = value, onValueChange = { config, value ->
                viewModel.setUserConfig(config, value)
            })
            if (index < viewModel.configItems.size - 1) {
                HorizontalDivider(
                    modifier = Modifier
                        .padding(horizontal = 10.dp)
                        .fillMaxWidth(),
                    thickness = 1.dp,
                    color = MaterialTheme.colorScheme.outline.copy(alpha = 0.5f)
                )
            }
        }
    }
}

@Preview
@Composable
fun ConfigScreenPreview() {
    SwordHolderApplication.previewContext = LocalContext.current
    SwordHolderTheme {
        ConfigScreen(modifier = Modifier.fillMaxSize())
    }
}